﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace CollisionDetectionSandbox
{
    class SplashScreen : Screen
    {
        Texture2D mWinScreen, mLoseScreen;
        Button mBackButton, mQuitButton, mBackHover, mQuitHover;
        public static int count;

        SoundEffect BGMusic;
        public static SoundEffectInstance MusicPlaying;


        Keys[] keysToCheck = new Keys[] {
        Keys.A, Keys.B, Keys.C, Keys.D, Keys.E,
        Keys.F, Keys.G, Keys.H, Keys.I, Keys.J,
        Keys.K, Keys.L, Keys.M, Keys.N, Keys.O,
        Keys.P, Keys.Q, Keys.R, Keys.S, Keys.T,
        Keys.U, Keys.V, Keys.W, Keys.X, Keys.Y,
        Keys.Z, Keys.Back, Keys.Space, Keys.Enter };

        KeyboardState currentKeyboardState;
        KeyboardState lastKeyboardState;

        public static bool enterPressed = false;
        public static bool flag = false;
        public static string text = "";
        SpriteFont Font1;
        Vector2 FontPos0;
        Vector2 FontPos1;
        Vector2 FontPos2;
        Vector2 FontPos3;
        Vector2 FontPos4;
        Vector2 FontPos5;

        

         public SplashScreen(ContentManager theContent, EventHandler theScreenEvent, String background)
            : base(theScreenEvent)
        {
            //Load the background texture for the screen
            Font1 = theContent.Load<SpriteFont>("Font/Font1");
            FontPos0 = new Vector2(GameScreen.GAME_WIDTH/3, 275);
            FontPos1 = new Vector2(GameScreen.GAME_WIDTH/2, 100);
            FontPos2 = new Vector2(GameScreen.GAME_WIDTH/2, 120);
            FontPos3 = new Vector2(GameScreen.GAME_WIDTH/3, 250);
            FontPos4 = new Vector2(GameScreen.GAME_WIDTH / 3, 300);
            FontPos5 = new Vector2(GameScreen.GAME_WIDTH / 3, 325);
            mWinScreen = theContent.Load<Texture2D>("Main Textures/WinScreen");
            mLoseScreen = theContent.Load<Texture2D>("Main Textures/LoseScreen");
            mBackButton = new Button(theContent, "ButtonTextures/button_menu2", ZeusGame.xpos - 100, ZeusGame.ypos + 250);
            mQuitButton = new Button(theContent, "ButtonTextures/button_quit2", ZeusGame.xpos + 100, ZeusGame.ypos + 250);
            mBackHover = new Button(theContent, "ButtonTextures/buttonhover_menu2", ZeusGame.xpos - 100, ZeusGame.ypos + 250);
            mQuitHover = new Button(theContent, "ButtonTextures/buttonhover_quit2", ZeusGame.xpos + 100, ZeusGame.ypos + 250);
            BGMusic = theContent.Load<SoundEffect>("Sounds/startscreen_backup");
            MusicPlaying = BGMusic.CreateInstance();
            MusicPlaying.Volume = 1.0f;

         }

         public override void Update(GameTime theTime)
         {
             if (MusicPlaying.State == SoundState.Stopped)
             {
                 //MusicPlaying.IsLooped = true;
                 MusicPlaying.Play();
             }

             MouseState state = Mouse.GetState();
             int mx = state.X;
             int my = state.Y;
             if (ZeusGame.outcome == true || ZeusGame.outcome == false)
             {                 
                 currentKeyboardState = Keyboard.GetState();
                 foreach (Keys key in keysToCheck)
                 {
                     if (CheckKey(key))
                     {
                         AddKeyToText(key);
                         break;
                     }
                 }

                 if (enterPressed == true)
                 {
                     ScoreUpdater su = new ScoreUpdater();
                     su.addNewScore(ZeusGame.score, text);
                     enterPressed = false;
                     flag = true;
                 }

                 if (state.LeftButton == ButtonState.Pressed)
                 {                     
                     if (mBackButton.hit_image(mx, my))
                     {
                         MusicPlaying.Stop();
                         enterPressed = false;
                         count = 1;
                         ScreenEvent.Invoke(this, new EventArgs());
                     }
                     else if (mQuitButton.hit_image(mx, my))
                     {
                         MusicPlaying.Stop();
                         enterPressed = false;
                         count = 2;
                         ScreenEvent.Invoke(this, new EventArgs());
                     }

                 }
                 
                 base.Update(theTime);
                 lastKeyboardState = currentKeyboardState;
             }
             
         }

         public override void Draw(SpriteBatch theBatch)
         {
             MouseState state = Mouse.GetState();
             int mx = state.X;
             int my = state.Y;

             if (ZeusGame.outcome == true)
             {
                 theBatch.Draw(mWinScreen, Vector2.Zero, Color.White);
                 //string one = "Congratulations!";
                 //string two = "You have successfully defended Mount Olympus from the horde";
                 string three = "Type your name and press Enter.";
                 Vector2 FontOrigin0 = Font1.MeasureString(text) / 2;
                 //Vector2 FontOrigin1 = Font1.MeasureString(one) / 2;
                 //Vector2 FontOrigin2 = Font1.MeasureString(two) / 2;
                 Vector2 FontOrigin3 = Font1.MeasureString(three) / 2;
                 Vector2 FontOrigin4 = Font1.MeasureString("Score - " + ZeusGame.score.ToString()) / 2;
                 Vector2 NameBackgroundPos = new Vector2(GameScreen.GAME_WIDTH - 92, 48);

                 //theBatch.DrawString(Font1, one, FontPos1, Color.Maroon,
                 //0, FontOrigin1, 1.0f, SpriteEffects.None, 0.5f);
                 //theBatch.DrawString(Font1, two, FontPos2, Color.Maroon,
                 //0, FontOrigin2, 1.0f, SpriteEffects.None, 0.5f);
                 theBatch.DrawString(Font1, three, FontPos3, Color.Black,
                 0, FontOrigin3, 1.0f, SpriteEffects.None, 0.5f);
                 theBatch.DrawString(Font1, "Score - " + ZeusGame.score.ToString(), FontPos4, Color.Black,
                 0, FontOrigin4, 1.0f, SpriteEffects.None, 0.5f);
                 theBatch.DrawString(Font1, text, FontPos0, Color.Black,
                 0, FontOrigin0, 1.0f, SpriteEffects.None, 0.5f);
             }
             else if (ZeusGame.outcome == false)
             {
                 theBatch.Draw(mLoseScreen, Vector2.Zero, Color.White);
                 //string one = "You were defeated..";
                 //string two = "Though you put up a valiant effort, Mount Olympus was overrun";
                 string three = "Type your name and press Enter.";
                 Vector2 FontOrigin0 = Font1.MeasureString(text) / 2;
                 //Vector2 FontOrigin1 = Font1.MeasureString(one) / 2;
                 //Vector2 FontOrigin2 = Font1.MeasureString(two) / 2;
                 Vector2 FontOrigin3 = Font1.MeasureString(three) / 2;
                 Vector2 FontOrigin4 = Font1.MeasureString("Score - " + ZeusGame.score.ToString()) / 2;
                 Vector2 NameBackgroundPos = new Vector2(GameScreen.GAME_WIDTH - 92, 48);

                 //theBatch.DrawString(Font1, one, FontPos1, Color.Maroon,
                 //0, FontOrigin1, 1.0f, SpriteEffects.None, 0.5f);
                 //theBatch.DrawString(Font1, two, FontPos2, Color.Maroon,
                 //0, FontOrigin2, 1.0f, SpriteEffects.None, 0.5f);
                 theBatch.DrawString(Font1, three, FontPos3, Color.White,
                 0, FontOrigin3, 1.0f, SpriteEffects.None, 0.5f);
                 theBatch.DrawString(Font1, "Score - " + ZeusGame.score.ToString(), FontPos4, Color.White,
                 0, FontOrigin4, 1.0f, SpriteEffects.None, 0.5f);
                 theBatch.DrawString(Font1, text, FontPos0, Color.White,
                 0, FontOrigin0, 1.0f, SpriteEffects.None, 0.5f);
             }
             if (flag == true)
                 {
                     if (ScoreUpdater.newHighScore == true && ZeusGame.outcome == false)
                     {
                         string four = "You have achieved a new high score!";
                         Vector2 FontOrigin5 = Font1.MeasureString(four) / 2;
                         theBatch.DrawString(Font1, four, FontPos5, Color.White,
                         0, FontOrigin5, 1.0f, SpriteEffects.None, 0.5f);
                     }
                     else if (ScoreUpdater.newHighScore == true && ZeusGame.outcome == true)
                     {
                         string four = "You have achieved a new high score!";
                         Vector2 FontOrigin5 = Font1.MeasureString(four) / 2;
                         theBatch.DrawString(Font1, four, FontPos5, Color.Black,
                         0, FontOrigin5, 1.0f, SpriteEffects.None, 0.5f);
                     }
                     else if(ScoreUpdater.newHighScore == false && ZeusGame.outcome == false)
                     {
                         string five = "A valiant effort, but no high score.";
                         Vector2 FontOrigin6 = Font1.MeasureString(five) / 2;
                         theBatch.DrawString(Font1, five, FontPos5, Color.White,
                         0, FontOrigin6, 1.0f, SpriteEffects.None, 0.5f);
                     }
                     else if (ScoreUpdater.newHighScore == false && ZeusGame.outcome == true)
                     {
                         string five = "A valiant effort, but no high score.";
                         Vector2 FontOrigin6 = Font1.MeasureString(five) / 2;
                         theBatch.DrawString(Font1, five, FontPos5, Color.Black,
                         0, FontOrigin6, 1.0f, SpriteEffects.None, 0.5f);
                     }
                 }

             if (mBackButton.hit_image(mx, my))
             {
                 mBackHover.Draw(theBatch);
             }
             else mBackButton.Draw(theBatch);

             if (mQuitButton.hit_image(mx, my))
             {
                 mQuitHover.Draw(theBatch);
             }
             else mQuitButton.Draw(theBatch);

             base.Draw(theBatch);
         }

         private void AddKeyToText(Keys key)
         {
             string newChar = "";
             if (text.Length >= 20 && key != Keys.Back)
                 return;
             if (key == Keys.Enter)
             {
                 enterPressed = true;
             }
             switch (key)
             {
                 case Keys.A:
                     newChar += "a";
                     break;
                 case Keys.B:
                     newChar += "b";
                     break;
                 case Keys.C:
                     newChar += "c";
                     break;
                 case Keys.D:
                     newChar += "d";
                     break;
                 case Keys.E:
                     newChar += "e";
                     break;
                 case Keys.F:
                     newChar += "f";
                     break;
                 case Keys.G:
                     newChar += "g";
                     break;
                 case Keys.H:
                     newChar += "h";
                     break;
                 case Keys.I:
                     newChar += "i";
                     break;
                 case Keys.J:
                     newChar += "j";
                     break;
                 case Keys.K:
                     newChar += "k";
                     break;
                 case Keys.L:
                     newChar += "l";
                     break;
                 case Keys.M:
                     newChar += "m";
                     break;
                 case Keys.N:
                     newChar += "n";
                     break;
                 case Keys.O:
                     newChar += "o";
                     break;
                 case Keys.P:
                     newChar += "p";
                     break;
                 case Keys.Q:
                     newChar += "q";
                     break;
                 case Keys.R:
                     newChar += "r";
                     break;
                 case Keys.S:
                     newChar += "s";
                     break;
                 case Keys.T:
                     newChar += "t";
                     break;
                 case Keys.U:
                     newChar += "u";
                     break;
                 case Keys.V:
                     newChar += "v";
                     break;
                 case Keys.W:
                     newChar += "w";
                     break;
                 case Keys.X:
                     newChar += "x";
                     break;
                 case Keys.Y:
                     newChar += "y";
                     break;
                 case Keys.Z:
                     newChar += "z";
                     break;
                 case Keys.Space:
                     newChar += " ";
                     break;
                 case Keys.Back:
                     if (text.Length != 0)
                         text = text.Remove(text.Length - 1);
                     return;
             }
             if (currentKeyboardState.IsKeyDown(Keys.RightShift) ||
                 currentKeyboardState.IsKeyDown(Keys.LeftShift))
             {
                 newChar = newChar.ToUpper();
             }
             if (enterPressed != true)
             {
                 text += newChar;
             }
         }

         private bool CheckKey(Keys theKey)
         {
             return lastKeyboardState.IsKeyDown(theKey) && currentKeyboardState.IsKeyUp(theKey);
         }
    }
}
